package com.huluobo.a2204kotlinjetpackdemo.paging

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.a2204kotlinjetpackdemo.R
import com.huluobo.a2204kotlinjetpackdemo.databinding.ActivityPagingBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * Paging总结:
 * paging 是page的进行时,也就是分页,之前SmartRefreshLayout还是其他的下拉刷新上拉加载框架,都是用于页面的分页加载
 * 有footer和header,用于加载或者刷新,通过footer加载数据,page++(视频接口,商品接口,currentPage++)
 * paging3是google退出的分页加载库,主要用于无感加载
 *
 * 1.Paging最关键的数据传递 Repo.getPagingData().cachedIn(viewModelScope) 生成一个数据流,在activity中可以通过
 *    viewModel.getPagingData().collect 来接收数据流).cachedIn(viewModelScope) 可以将请求到的数据缓存到作用域中,如果当生命周期
 *    发生改变的时候,可以读取这部分数据
 * 2. PagingConfig(pageSize = PAGE_SIZE, prefetchDistance = PAGE_SIZE * 5),来设置预加载的大小,
 * 3.RepoPagingSource 重写load方法,加载数据,请求接口
 * 4.RepoPagingSource 才是真正产生数据源的位置,在这当中触发了网络请求
 * 5.PagingAdapter 是配合paging使用的,构造中需要有个DiffUtil 来管理item中的数据是否一致
 * 6.LoadStateAdapter 是一个脚布局,主要作用是有个一个状态state,通过这个状态可以监听paging加载的情况比如成功,加载中,加载错误,类似LiveData
 * 7.paging给adapter传递数据的位置是               adapter.submitData(it),通过协程的flow流从viewModel中拿到数据并填充
 */
class PagingActivity : AppCompatActivity() {
    private lateinit var binding: ActivityPagingBinding
    private val adapter = PagingAdapter()
    private val viewModel by lazy { ViewModelProvider(this)[PagingViewModel::class.java] }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPagingBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.pagingRv.layoutManager = LinearLayoutManager(this)
        binding.pagingRv.adapter = adapter.withLoadStateFooter(FooterAdapter { adapter.retry() })

        lifecycleScope.launch {
            viewModel.getPagingData().collect {
                //**********paging加载的重点,通过协程发送和棘手数据,给adapter添加数据源
                adapter.submitData(it)
            }
        }

        //给adapter添加加载状态的监听器
        adapter.addLoadStateListener {
            when (it.refresh) {
                is LoadState.NotLoading -> {
                    binding.pagingRv.visibility = View.VISIBLE
                    binding.pagingTv.visibility = View.GONE
                }

                is LoadState.Loading -> {
                    binding.pagingRv.visibility = View.VISIBLE
                    binding.pagingTv.visibility = View.GONE
                }

                is LoadState.Error -> {
                    val state = it.refresh as LoadState.Error
                    binding.pagingRv.visibility = View.GONE
                    binding.pagingTv.visibility = View.VISIBLE
                    binding.pagingTv.text = state.error.message
                }
            }
        }
    }
}